<?php

namespace app\api\controller;

use app\admin\model\Article as ArticleModel;
use app\admin\model\Banner as BannerModel;
use app\admin\model\About as AboutModel;
use think\facade\Lang;
use think\Cache;
use app\common\Status;

class Article extends Base
{

    /**
     * 获取文章列表
     * @return \think\response\Json
     */
    public function getArticle()
    {
        try {
            $page = intval(input('param.page', 1));
            $size = intval(input('param.size', 15));
            $language = isset($this->header['accept-language']) ? $this->header['accept-language'] : 0;
            $where = ['language' => $language];
            $infos = [];
            $filed = ['id', 'title', 'add_time', 'introduction', 'cover_image'];
            $list = (new ArticleModel())->articleList($where, $page, $size, $filed);
            foreach ($list as $key => $item) {
                $infos[$key]['id'] = $item['id'];
                $infos[$key]['title'] = $item['title'];
                $infos[$key]['addTime'] = $item['add_time'];
                $infos[$key]['introduction'] = $item['introduction'];
                $infos[$key]['coverImage'] = env('APP_DOMAIN') . $item['cover_image'];
            }

            return returnJson(Status::SUCCESS, '', $infos);
        } catch (\Exception $e) {
            errorLog($this->action, $e);
            return returnJson(Status::SYSTEM_ERROR, Lang::get('system_error'));
        }
    }

    /**
     * 获取文章详情
     * @return \think\response\Json
     */
    public function getDetail()
    {
        try {
            $id = intval(input('param.id'));
            if (empty($id)) {
                return returnJson(Status::FAILED, Lang::get('not_null'));
            }
            $where = ['id' => $id];
            $filed = ['id', 'title', 'add_time', 'content', 'author', 'cover_image'];
            $info = (new ArticleModel())->getOne($where, $filed);
            if (empty($info)) {
                $list = [];
            } else {
                $list = [
                    'id' => $info['id'],
                    'title' => $info['title'],
                    'addTime' => $info['add_time'],
                    'content' => $info['content'],
                    'author' => $info['author'],
                    'coverImage' => env('APP_DOMAIN') . $info['cover_image'],
                ];
            }

            return returnJson(Status::SUCCESS, '', $list);
        } catch (\Exception $e) {
            errorLog($this->action, $e);
            return returnJson(Status::SYSTEM_ERROR, Lang::get('system_error'));
        }
    }

    /**
     * 首页banner
     * @return \think\response\Json
     */
    public function getBanner()
    {
        try {
            $id = intval(input('param.id', 1));
            $where = ['location' => $id, 'language' => $this->lang_type];
            $filed = ['id', 'banner_image', 'app_url as jump_url'];
            $list = (new BannerModel())->bannerList($where, $filed);
            $result = [];
            foreach ($list as $key => $value) {
                $result[$key]['bannerImage'] = env('APP_DOMAIN') . $value['banner_image'];
                $result[$key]['jumpUrl'] = json_decode($value['jump_url']);
            }
            return returnJson(Status::SUCCESS, '', $result);
        } catch (\Exception $e) {
            errorLog($this->action, $e);
            return returnJson(Status::SYSTEM_ERROR, Lang::get('system_error'));
        }
    }

    /**
     * @Title: about
     * @Describe 首页公告
     * @return \think\response\Json
     * @author lijunwei
     * @Date 2021/9/28 16:07
     */
    public function about()
    {
        try {
            $language = isset($this->header['accept-language']) ? $this->header['accept-language'] : 0;
            $where = ['language' => $language, 'type' => 1];
            $filed = ['id', 'title', 'content', 'created_time'];
            $list = (new AboutModel())->articleList($where, 1, 5, $filed);
            if (!$list) {
                return returnJson(Status::FAILED, Lang::get('no_data'));
            }

            return returnJson(Status::SUCCESS, '', $list);
        } catch (\Exception $e) {
            errorLog($this->action, $e);
            return returnJson(Status::SYSTEM_ERROR, Lang::get('system_error'));
        }
    }

    /**
     * @Title: aboutList
     * @Describe  公告列表
     * @return \think\response\Json
     * @author lijunwei
     * @Date 2021/9/28 16:26
     */
    public function aboutList()
    {
        try {
            $page = intval(input('param.page', 1));
            $size = intval(input('param.size', 15));
            $language = isset($this->header['accept-language']) ? $this->header['accept-language'] : 0;
            $where = ['language' => $language, 'type' => 1];
            $filed = ['id', 'title', 'content', 'created_time'];
            $list = (new AboutModel())->articleList($where, $page, $size, $filed);
            if (!$list) {
                return returnJson(Status::FAILED, Lang::get('no_data'));
            }
            return returnJson(Status::SUCCESS, '', $list);
        } catch (\Exception $e) {
            errorLog($this->action, $e);
            return returnJson(Status::SYSTEM_ERROR, Lang::get('system_error'));
        }
    }

    /**
     * 获取公告详情
     * @return \think\response\Json
     */
    public function getAboutDetail()
    {
        try {
            $id = intval(input('param.id'));
            if (empty($id)) {
                return returnJson(Status::FAILED, Lang::get('not_null'));
            }
            $where = ['id' => $id];
            $filed = ['id', 'title', 'pub_time', 'content', 'author', 'cover_image'];
            $info = (new AboutModel())->getOne($where, $filed);
            $info['cover_image'] = env('APP_DOMAIN') . $info['cover_image'];

            return returnJson(Status::SUCCESS, '', $info);
        } catch (\Exception $e) {
            errorLog($this->action, $e);
            return returnJson(Status::SYSTEM_ERROR, Lang::get('system_error'));
        }
    }

    /**
     * @Title: financeNews
     * @Describe 财经新闻
     * @return \think\response\Json
     * @author lijunwei
     * @Date 2021/11/4 17:31
     */
    public function financeNews()
    {
        try {
            $page = intval(input('param.page', 1));
            $size = intval(input('param.size', 15));
            $language = isset($this->header['accept-language']) ? $this->header['accept-language'] : 0;
            $where = ['language' => $language, 'type' => 2];
            $filed = ['id', 'content', 'created_time'];
            $list = (new AboutModel())->articleList($where, $page, $size, $filed);
            if (!$list) {
                return returnJson(Status::FAILED, Lang::get('no_data'));
            }
            return returnJson(Status::SUCCESS, '', $list);
        } catch (\Exception $e) {
            errorLog($this->action, $e);
            return returnJson(Status::SYSTEM_ERROR, Lang::get('system_error'));
        }
    }
}
